var header = (function(){
	var USER_ID = 1234;
	
	var Data = {
			items : []
	};
	
	var _init = function(){
		var KEYWORD = Util.Url.GetArgsFromHref("this", "keyWord");
		$('#indexKeyWord').val(decodeURI(KEYWORD));
		_loadCommonInfo();
		_addEvent();
		_showUserInfo();
	};
	
	/**
	 * 加载头部、底部公共信息
	 */

	var _loadCommonInfo = function(){
		var par = {};
		par.userId = USER_ID;
		Util.ajax.postJson(srvMap.get('queryIdxHeadMemu'),par,function(json,status){
			if(status){
				_assembleHeadInfo(json.object);
				_assembleFootInfo(json.beans);
				$('#copyRightInfo').find('span').text(json.bean.modeName);
			}
		});
	};

	var _assembleHeadInfo = function(datas){
		if(!!datas){
			var $ul = $('#guider_info_ul');
			$ul.empty();
			for (var i = 0; i < datas.length; i++) {
				//1
				var menu = datas[i];
				if(! menu.child || menu.child == '' || menu.child == null){
					var $li = $('<li class="" data="'+menu.modeUrl+'"><a target="_blank">'+menu.modeName+'</a></li>');
					$ul.append($li);
					
					$li.click(function(){
						var pageUrl = $(this).attr('data');
						location.href = pageUrl;
					});
				}else{
					var $li = $('<li class="" name="chosseli_new">');
					var $a = $('<a>'+menu.modeName+'<div class="guider_info_ul_down"></div></a>');
					var $div = $('<div class="guider_info_ul_downbox8x"></div>');
					var $dt = $('<dt class="guider_info_ul_downbox">');
					var childs = menu.child;
					for (var k = 0; k < childs.length; k++) {
						//2
						var child = childs[k];
						var $dl = $('<dl class="downbox_dlnav">');
						var $h1 = $('<h1>'+child.modeName+'</h1>');
						//3
						var sub = child.child;
						if(!!sub){
							for (var j = 0; j < sub.length; j++) {
								var subChild = sub[j];
								var $aa = $('<a data="'+subChild.modeUrl+'"><dd><i class="fa_icon" ></i>'+subChild.modeName+'</dd></a>');
								$h1.append($aa);
								
								$aa.click(function(){
									var pageUrl = $(this).attr('data');
									location.href = pageUrl;
								});
							}
						}
						$dl.append($h1);
						$dt.append($dl);
					}
					$li.append($a).append($div).append($dt);
					$ul.append($li);
					
					$li.click(function(event){
						event.stopPropagation();
						$li.parent().find('li').removeClass('choose_hover');
						if($(this).hasClass('choose_hover')){
							$(this).removeClass('choose_hover');
						}else{
							$(this).addClass('choose_hover');
						}
					});
					
//					$(document.body).not($li).click(function(){
//						$li.removeClass('choose_hover');
//				    });
				}
			}
		}
	};

	var _assembleFootInfo = function(datas){
		var $footInfo = $('#footInfo');
		$footInfo.empty();
		for (var i = 0; i < datas.length; i++) {
			var foot = datas[i];
			var $span = $('<span><a href="'+foot.modeUrl+'" value="'+foot.modeId+'">'+foot.modeName+'</a></span>');
			$footInfo.append($span);
		}
	};
	
	var _showUserInfo = function(){
		var userName = '测试用户';//暂时写死，从session中获取
		if(userName == null || userName == ''){
			$('#span_uname').text('未登录');
		}else{
			$('#span_uname').text(userName);
		}
	};

	var _addEvent = function(){
		$(document.body).not($('#guider_info_ul')).click(function(){
			$('#guider_info_ul').children('li').removeClass('choose_hover');
	    });
	    $(document).not($("#search_guild_icon")).click(function(){
	        $("#search_guild_icon").removeClass('search_guild_show');
			$("#search_guild_icon").addClass('search_guild_icon');
	    });
	    /*防止事件冒泡*/
	    $("#search_guild_icon").click(function(event){
	        event.stopPropagation();
	    });
	    
	    $("#searchBar").click(function(event){
	        _searchIndexInfoByKeyWord();
	    });
	    
	    $("#indexKeyWord").keyup(function(event){
	    	if(event.keyCode == 13){
	    		_searchIndexInfoByKeyWord();
	        }
	    });
	};
	
	//根据关键字查询指标信息
	var _searchIndexInfoByKeyWord = function(){
		var keyWord = $.trim($('#indexKeyWord').val()); //查询关键字
		keyWord = _screenKeyWord(keyWord);
//		if(keyWord == ""){
//			return;
//		}
		var URL = decodeURI(window.location.href);
		var URLName = URL.split('/');
		var pageName = URLName[URLName.length-1];
		URLName = pageName.split('.');
		pageName = URLName[0];
		if(pageName == "indicatorStore" ){
			Idx_Store.init(keyWord);
		}else{
			location.href = encodeURI("../../module/indexMarket/indicatorStore.html?keyWord="+keyWord);
		}
	};
	
	/**
	 * 对关键字进行筛选，去除其中的%等特殊字符，前台防止注入
	 * @param keyWord
	 */
	var _screenKeyWord = function(keyWord){
		if(keyWord == "请输入关键字"){
			keyWord = "";
		}
		var keyTemp = keyWord.replace(/[&\|\\\*^%$#@\-]/g,"");
		$('#indexKeyWord').val(keyTemp);
		return keyTemp;
	}
	
	return {
		init : _init //对外提供初始化方法
	}
})();



$(function(){
	//指标头部菜单展示
	srvMap.add('queryIdxHeadMemu', 'queryIdxHeadMemu.json','front/sh/common!execute?uid=common_001');
	header.init();
});
